package cn.bjxy.shishaolong.ra_room.service;

import cn.bjxy.shishaolong.ra_room.entity.RaRomDeleteRequest;
import cn.bjxy.shishaolong.ra_room.entity.RaroomRequestRalation;
import cn.bjxy.shishaolong.ra_room.entity.RaroomStudentRalation;
import cn.bjxy.shishaolong.ra_room.mapper.RaroomRequestRalationMapper;
import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.request_answer.mapper.AnswerMapper;
import cn.bjxy.shishaolong.request_answer.mapper.RequestMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 答疑室与问题关联 服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-04-13
 */
@Service
public class RaroomRequestRalationServiceImpl extends ServiceImpl<RaroomRequestRalationMapper, RaroomRequestRalation> implements IRaroomRequestRalationService {

    @Autowired
    private RaroomRequestRalationMapper requestRalationMapper;
    @Autowired
    private RequestMapper requestMapper;
    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public List<Request> queryRequestForRoomByCondition(Request request) {
        List<Request> requestList = requestMapper.queryRequestForRoomByCondition(request);
        setAnswerByRequestId(request, requestList);
        return requestList;
    }

    /**
     * 给问题设置回答
     * @param request
     * @param requestList
     */
    private void setAnswerByRequestId(Request request, List<Request> requestList) {
        if (requestList != null && requestList.size() > 0) {
            for (Request tempRequest : requestList) {
                List<Answer> answerList = answerMapper.queryByCondition(new Answer().setRequestId(tempRequest.getTid()));
                tempRequest.setAnswerList(answerList);
            }
        }
    }

    @Override
    public IPage<Request> queryRequestForRoomByCondition(IPage<Request> page, Request request) {
        List<Request> requestList = requestMapper.queryRequestForRoomByCondition(page, request);
        setAnswerByRequestId(request, requestList);
        return page.setRecords(requestList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void request(Request myRequest) {
        // 专业和课程填写成-1
        myRequest.setProfessionId(-1).setCourseId(-1);
        // 存数据表中
        requestMapper.insert(myRequest);

        // 存关联表
        requestRalationMapper.insert(new RaroomRequestRalation().setRequestId(myRequest.getTid())
                .setRoomId(myRequest.getRoomId()));
    }

    @Override
    public void remove(RaRomDeleteRequest deleteRequest) {
        if (1 == deleteRequest.getType()) {  // 问题
            // 删除关联表
            requestRalationMapper.update(new RaroomRequestRalation().setDeletedAt(LocalDateTime.now()),
                    new UpdateWrapper<RaroomRequestRalation>().lambda()
                            .eq(RaroomRequestRalation::getRoomId, deleteRequest.getRoomId())
                            .eq(RaroomRequestRalation::getRequestId, deleteRequest.getTid()));
            // 删除数据表
            requestMapper.update(new Request().setDeletedAt(LocalDateTime.now()),
                    new UpdateWrapper<Request>().lambda().eq(Request::getTid, deleteRequest.getTid()));
        } else { // 回答
            // 删除回答数据表
            answerMapper.update(new Answer().setDeletedAt(LocalDateTime.now()),
                    new UpdateWrapper<Answer>().lambda().eq(Answer::getTid, deleteRequest.getTid()));
        }
    }
}
